package com.ljw.community.controller;

import com.ljw.community.entity.TopicConsts;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class BasicController {
    protected String uploadFile(String uploadDir, MultipartFile f) {
        String realPath = TopicConsts.getUploadPath();
        String uploadPath = uploadDir + UUID.randomUUID() + "." + f.getContentType().split("/")[1];
        File file = new File(realPath + uploadPath);
        if (!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        try {
            if (!f.isEmpty()){
                if (f.getSize()>0){
                    f.transferTo(file);
                    System.out.println("图片已保存到 "+realPath+uploadPath);
                    return uploadPath;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
